// +----------------------------------------------------------------------
// | ADM Software [ 我宁可 为做过的事而后悔，也不想 因为没尝试而后悔。 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 扬州安蝶梦电子商务有限公司 All rights reserved.
// +----------------------------------------------------------------------
// | Author: 曹梦龙 <138888611@qq.com> <http://www.andiemeng.com>
// +----------------------------------------------------------------------
package com.gr100.biz.kit.bmap;

import java.io.IOException;
import java.net.URLEncoder;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import com.alibaba.fastjson.JSONObject;

/*
 * 百度中文地址逆推 坐标方法
 */
public class GeoCoder {

	private final static OkHttpClient client = new OkHttpClient();
	/*
	 * 传人中文地址、和城市 返回 fastjson 格式json 对象包含经纬度和可信度
	 */
	public static JSONObject Geocoding(String address,String city){

		JSONObject json = new JSONObject();
		try {
			address = URLEncoder.encode(address, "utf-8");
			city = URLEncoder.encode(city, "utf-8");
			String url="http://api.map.baidu.com/geocoder/v2/?ak=W7fSw2UYZWEZsT8sr0jH6OuF&output=json&address=" + address + "&city=" + city;
			Request request = new Request.Builder()
					.url(url)
					.build();

			Response response = client.newCall(request).execute();
			if (!response.isSuccessful())
				throw new IOException("HTTP请求错误");

			json = JSONObject.parseObject(response.body().string());

		} catch (IOException e) {
			json.put("status", -1);
			json.put("message","发生错误:\r\n可能是：" + e.getMessage());
		}
		return  json;
	}
	
}
